//
//  SixteenIndexesViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/15.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class SixteenIndexTitleViewController: UIViewController {
    fileprivate let cellId = "sixteen_indexTitle_cell";
    fileprivate lazy var mainTableView:UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: screenMainSize.height), style: UITableView.Style.grouped);
        tableView.backgroundColor = .white;
        tableView.showsVerticalScrollIndicator = false;
        tableView.showsHorizontalScrollIndicator = false;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId);
        tableView.sectionIndexColor = .purple;//索引颜色
        return tableView;
    }();
        
    fileprivate var dataArray:NSMutableArray = {
        var mutArr = NSMutableArray();
        
        let array = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","L","S","T","U","V","w","X","Y","Z"];
        for inde in 0...array.count-1 {
            var itemArr = NSMutableArray();
            for index in 0...5 {
                var indexString = array[inde];
                if  indexString == "H" || indexString == "M" {
                    continue;
                }
                indexString = "\(indexString)-\(index)";
                itemArr.add(indexString);
            }
            mutArr.add(["title":array[inde],"data":itemArr]);
        }
        
        return mutArr;
    }();
        
    fileprivate lazy var indexTitleArr:NSMutableArray = {
        var mutArr = NSMutableArray();
        let array = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","L","S","T","U","V","w","X","Y","Z"];
        mutArr.addObjects(from: array);
        return mutArr;
    }();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "表格索引和删除";
        // Do any additional setup after loading the view.
        self.onCreatUIForNivation();
        self.onCreatUIForView();
    }
    
    //TODO:UI
    fileprivate func onCreatUIForNivation() {
//        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .done, target: self, action: #selector(onBackAction));
    }
    
    fileprivate func onCreatUIForView() {
        self.view.backgroundColor = .white;
        
        self.view.addSubview(self.mainTableView);
    }

    //TODO:Actions
    @objc fileprivate func onBackAction() {
        self.navigationController?.dismiss(animated: true, completion: nil);
    }
}

extension SixteenIndexTitleViewController:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataArray.count;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.dataArray.count > section {
            let dataDic:Dictionary<String,AnyObject> = self.dataArray.object(at: section) as! Dictionary<String, AnyObject>;
            let array:Array<String> = dataDic["data"] as! Array<String>;
            return array.count;
        }
        return 0;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId)!;
        cell.backgroundColor = .lightGray;
        
        if self.dataArray.count > indexPath.section {
            let dataDic:Dictionary<String,AnyObject> = self.dataArray.object(at: indexPath.section) as! Dictionary<String, AnyObject>;
            let array:Array<String> = dataDic["data"] as! Array<String>;
            if array.count > indexPath.row {
                cell.textLabel?.text = array[indexPath.row];
            }
        }
        
        return cell;
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: 40));
        
        let titleLb = UILabel(frame: CGRect(x: 20, y: 0, width: headerView.frame.width-40, height: headerView.frame.height));
        titleLb.textAlignment = .left;
        titleLb.textColor = .black;
        headerView.addSubview(titleLb);
        if self.dataArray.count > section {
            let dataDic:Dictionary<String,AnyObject> = self.dataArray.object(at: section) as! Dictionary<String, AnyObject>;
            let dataString:String = dataDic["title"] as! String;
            titleLb.text = dataString;
        }
        
        return headerView;
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40;
    }
    
    //右侧索引
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return (self.indexTitleArr as! [String]);
    }
    //索引和section关联
    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        guard let inde:Int = self.indexTitleArr.index(of: title) else {
            return -1;
        }
        return inde;
    }
    
    //cell删除
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true;
    }
    
    //单个删除
//    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
//        return "走你";
//    }
    
    //多个删除
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
        let action1 = UIContextualAction(style: .destructive, title: "first blood") { (action, view, completeHandle) in
            print("gogo");
            completeHandle(true);
        }
        action1.image = UIImage(named: "watch_scanner_fragment_icon");
        let action2 = UIContextualAction(style: .destructive, title: "second blood") { (action, view, completeHandle) in
            print("gogo2");
            completeHandle(true);
        }
        action2.backgroundColor = .green;
        
        let configuration = UISwipeActionsConfiguration(actions: [action1,action2])
        return configuration;
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if self.dataArray.count > indexPath.section {
            let dataDic:Dictionary<String,AnyObject> = self.dataArray[indexPath.section] as! Dictionary<String,AnyObject>;
            let array:Array<String> = dataDic["data"] as! Array<String>;
            if array.count > indexPath.row {
                let mutArr = NSMutableArray(array: array);
                mutArr.removeObject(at: indexPath.row);
                
                let mutDataDic:NSMutableDictionary = NSMutableDictionary(dictionary: dataDic);
                mutDataDic.setValue(mutArr, forKey: "data");
                self.dataArray.replaceObject(at: indexPath.section, with: mutDataDic);
                
            }
            tableView.deleteRows(at: [indexPath], with: .fade);
        }
    }
}
